package com.kai.mqtt.handler;

import com.kai.mqtt.statistics.IStatisticsService;
import io.netty.channel.*;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author weijk
 * @since 2024/7/1
 */
@Scope("prototype")
@Component
public class MsgCountHandler extends ChannelDuplexHandler {

    @Resource
    private IStatisticsService statisticsCacheService;

    @Override
    public void read(ChannelHandlerContext ctx) throws Exception {
        statisticsCacheService.incrementMsgInflow();
        ctx.read();
    }

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        statisticsCacheService.incrementMsgOutflow();
        ctx.write(msg, promise);
    }
}
